<?php

namespace App\Http\Controllers\api;

use App\Events\WorkshopCreated;
use App\Events\WorkshopCreatedEvent;
use App\Events\WorkshopShenhe;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\DiskSourceCreatedRequest;
use App\Http\Resources\Industry;
use App\Http\Resources\panyuan\MyWorkshopCollection;
use App\Http\Resources\panyuan\UserShenheResource;
use App\Http\Resources\panyuan\UserWorkshopCollection;
use App\Http\Resources\panyuan\WorkshopCollection;
use App\Http\Resources\panyuan\WorkshopDetailResource;
use App\Http\Resources\panyuan\WorkshopOverdueCollection;
use App\Http\Resources\UserShenheCollection;
use App\Http\Resources\WorkShopResource;
use App\Interfaces\WorkshopInterFace;
use App\Jobs\SendUserMini;
use App\model\City;
use App\model\Disksource;
use App\model\panyuan\MyApply;
use App\model\panyuan\UserShenheLog;
use App\model\panyuan\UserShenheSum;
use App\model\panyuan\WorkshopLog;
use App\model\panyuan\WorkshopOverdue;
use App\model\User;
use App\model\UserWorkshop;
use App\model\UserWorkshopApply;
use App\model\Workshop;
use App\model\WorkshopContact;
use App\model\WorkshopHangye;
use App\model\WorkshopIndustry;
use App\model\WorkshopTag;
use App\model\WorkshopWeight;
use App\Repository\WorkShopRepository;
use App\UserShenhe;
use http\Env\Response;
use http\QueryString;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;


class DiskSourceController extends Controller
{
    /**
     * @param Request $request
     * @return MyWorkshopCollection
     */
    public function publishList(Request $request){
        $user=$request->user('api');
        $shenhe=$request->shenhe;
        $search_key=$request->search_key;
        $time=$request->time;


       $workshop= Workshop::withoutGlobalScope('shenhe');
       if ($user->role_id==3) {
           $workshop = $workshop->where('user_id', '=', $user->id);
       }
       if ($request->time){
           switch ($request->time){
               case 'yesterday':
                $workshop=$workshop->where('created_at','>=',Carbon::yesterday())
                            ->where('created_at','<=',Carbon::today());
                break;
               case 'week':
                   $workshop=$workshop->where('created_at','>=',Carbon::now()->subWeek(0)->startOfWeek())
                       ->where('created_at','<=',Carbon::now());
                   break;
               case 'month':
                   $workshop=$workshop->where('created_at','>=',Carbon::now()->firstOfMonth())
                       ->where('created_at','<=',Carbon::now());
           }

       }
       $workshop=$workshop

            ->orderBy('updated_at','desc')

            ->with('tags');

       if ($shenhe!==null){
           $workshop=$workshop->where('shenhe',$shenhe);
       }
       if ($search_key){
           $workshop=$workshop->where('title','like','%'.$search_key.'%');
       }
        $workshop=$workshop->paginate(20);

        return new MyWorkshopCollection($workshop);
    }
    /**
     * 创建盘源
     * @param DiskSourceCreatedRequest $request
     * @param WorkshopInterFace $disSourceInterFace
     * @return \Illuminate\Http\JsonResponse
     * @throws ApiException
     */
    public function create(DiskSourceCreatedRequest $request, WorkshopInterFace $disSourceInterFace)
    {

        if (!$request->user('api')->can('created', Workshop::class)) {
            throw new ApiException('你没有权限创建盘源');
        }
        $diskSource = $disSourceInterFace->addDiskSource($request->user('api'), $request);
        $code=$disSourceInterFace->getCode($diskSource);
        //写入申请表
        $myapply=new MyApply();
        $myapply->user_id=$request->user('api')->id;
        $myapply->workshop_id=$diskSource->id;
        $myapply->status=MyApply::SHENHE;
        $myapply->type=MyApply::Created_panyuan;
        $myapply->code=$code;
        $myapply->save();
        //写入消息
        $userShenhe= new UserShenhe();
        $userShenhe->role_id=1;//采购审核
        $userShenhe->workshop_id=$diskSource->id;
        $userShenhe->type=UserShenhe::Created_panyuan;
        $userShenhe->status=UserShenhe::STATUS_SHENHE;
        $userShenhe->title=$request->user('api')->name.'有一条盘源发布待审核，请尽快处理';
        $userShenhe->code=$code;
        $userShenhe->user_id=$request->user('api')->id;
        $userShenhe->is_read=0;
        $userShenhe->save();
        if ($diskSource) {
            event(new WorkshopCreated($diskSource));
            $disSourceInterFace->CreateWorkshopNotice($diskSource,$code);
            return response()->json([
                'code' => 'success',
                'message' => '添加成功',
                'data' => $diskSource
            ]);
        }
        throw new ApiException('创建失败');
    }

    /**
     * @param $id
     * @param WorkshopInterFace $workshopInterFace
     * @param DiskSourceCreatedRequest $request
     * @throws ApiException
     */
    public function shenhe($id,WorkshopInterFace $workshopInterFace,Request $request){
        $userShenhe=UserShenhe::find($id);
         if ($userShenhe->status==UserShenhe::STATUS_TONGYI){
             throw new ApiException('已经同意');
         }
        switch ($userShenhe->type){
            case UserShenhe::Created_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                if (!$request->user('api')->can('shenhe', $workshop)) {
                    throw new ApiException('你没有权限审核');
                }
               $workshop= $workshopInterFace->diskSourcePassed($request->user('api'),$workshop,$userShenhe);
                break;
            case UserShenhe::View_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshop=$workshopInterFace->passView($request->user('api'),$userShenhe,$workshop);
                break;
            case UserShenhe::Updated_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshop=$workshopInterFace->updatedPanyuan($request->user('api'),$userShenhe,$workshop);
                break;
        }
        return response()->json([
            'code' => 'success',
            'message' => '审核成功',
            'data' => ''
        ]);
    }

    /**
     * 审核拒绝
     * @param $id
     * @param Request $request
     * @param WorkshopInterFace $workshopInterFace
     * @return \Illuminate\Http\JsonResponse
     * @throws ApiException
     */
    public function refuse($id,Request $request, WorkshopInterFace $workshopInterFace){

        $userShenhe=UserShenhe::find($id);
        if ($userShenhe->status==UserShenhe::STATUS_JUJUE){
            throw new ApiException('已经拒绝');
        }
        switch ($userShenhe->type){
            case UserShenhe::Created_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                if (!$request->user('api')->can('refuse', $workshop)) {
                    throw new ApiException('你没有权限审核盘源');
                }
                $workshopInterFace->diskSourceRefuse($request->user('api'),$workshop,$userShenhe,$request->refuse);
                break;
            case UserShenhe::View_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshopInterFace->viewSourceRefuse($request->user('api'),$workshop,$userShenhe,$request->refuse);
                break;
            case  UserShenhe::Updated_panyuan:
                $workshop=Workshop::withoutGlobalScope('shenhe')->find($userShenhe->workshop_id);
                $workshopInterFace->updateSourceRefuse($request->user('api'),$workshop,$userShenhe,$request->refuse);
                break;
        }
        UserShenheSum::addSum($userShenhe->user_id);

        return response()->json([
            'code' => 'success',
            'message' => '拒绝成功',
            'data' => $workshop
        ]);
    }
    public function updateList($id){
        $workshop=WorkshopLog::with('user')
            ->where('workshop_id',$id)
            ->orderBy('id','desc')
            ->get();
        $workshops=Workshop::find($id);
        return \response()->json([
            'code'=>'success',
            'message'=>'',
            'data'=>$workshop->map(function($item)use($workshops){
                return [
                   'time'=>Carbon::parse($item->created_at)->format("m-d"),
                   'user'=>$item->user,
                   'desc'=>$item->desc,
                    'status'=>$item->status,
                    'workshop'=>$workshops
                ];
            })
        ]);
    }
    public function getContact($workshopId)
    {
        if ($workshopId!=0){
            $workshopContact = WorkshopContact::where('workshop_id', $workshopId)->get();
        }else{
            $workshopContact=[];
        }

        return $workshopContact;
    }

    /**
     * @param Disksource $id
     * @param DiskSourceCreatedRequest $diskSourceCreatedRequest
     * @param WorkshopInterFace $workshopInterFace
     * @return \Illuminate\Http\JsonResponse
     * @throws ApiException
     * 编辑
     */
    public function edit( $id,DiskSourceCreatedRequest $diskSourceCreatedRequest,WorkshopInterFace $workshopInterFace){
        $user=$diskSourceCreatedRequest->user('api');
        $code=$diskSourceCreatedRequest->code;
        $workshop=Workshop::withoutGlobalScope('shenhe')->find($id);
        if (!$diskSourceCreatedRequest->user('api')->can('update',$workshop )) {
            throw new ApiException('你没有权限修改盘源');
        }
        if ($code){
           $myapply= MyApply::where('code',$code)->first();
           if ($myapply->type==MyApply::Created_panyuan){//创建房源重新编辑
                $workshop= $workshopInterFace->mangerUpdatedPanyuan($diskSourceCreatedRequest->all(),$workshop);
               $userShenhe= new UserShenhe();
               $myapply->status=MyApply::SHENHE;
               $myapply->save();
               $userShenhe->role_id=1;//采购审核
               $userShenhe->workshop_id=$workshop->id;
               $userShenhe->type=UserShenhe::Created_panyuan;
               $userShenhe->status=UserShenhe::STATUS_SHENHE;
               $userShenhe->title=$user->name.'有一条盘源发布待审核，请尽快处理';
               $userShenhe->code=$code;
               $userShenhe->user_id=$user->id;
               $userShenhe->is_read=0;
               $userShenhe->save();
               $workshopInterFace->CreateWorkshopNotice($workshop,$code);
               return response()->json([
                       'code' => 'success',
                       'message' => '编辑成功',
                   ]);

           }
        }
        $edit=$diskSourceCreatedRequest->edit;
        if ($edit==1&&$user->role_id!=3){//直接编辑通过
            $workshopInterFace->mangerUpdatedPanyuan($diskSourceCreatedRequest->all(),$workshop);
            return response()->json([
                'code'=>'success',
                'message'=>'编辑成功'
            ]);
        }
        if (WorkshopLog::where('workshop_id',$id)->where('status',WorkshopLog::STATUS_SHENHE)->first()){
            return response()->json([
                'code'=>'error',
                'message'=>'正在等待审核',
            ]);
        }

        $workshopInterFace->editDiskSource($diskSourceCreatedRequest->user('api'),$workshop,$diskSourceCreatedRequest);

        return response()->json([
            'code'=>'success',
            'message'=>'请等待审核'
        ]);
    }
    public function workshop($id){
        $workshop=Workshop::withoutGlobalScope('shenhe')->with('industry','hangye','weight_type')->where('id',$id)->first();

        $img=json_decode($workshop->slider,true);
        $industry= WorkshopIndustry::all();
        $hangye=WorkshopHangye::all();
        $chengzhong=WorkshopWeight::all();
        $city=   City::with('children')->whereIn('parent_id',[102,101])->get();
        $firstCity=$city->search(function($value,$index)use($workshop){
            return $value->id==$workshop->p_city_id;
        });
        $secondCity=$city[$firstCity]->children->search(function($value,$index)use($workshop){
            return $value->id==$workshop->p_district_id;
        });
        $onlyArray[]=$city->map(function ($item){
           return $item->title;
        });
        $onlyArray[]=$city[$firstCity]->children->map(function($item){
            return $item->title;
        });
        $industry_index=[];
        $industry_id=[];
        if ($workshop->industry->isNotEmpty()){
            $industry_id= $workshop->industry->pluck('id')->all();

            foreach ($industry as $k=> $item){
                if (in_array($item->id,$industry_id)){
                    $industry_index[$k]=true;
                }else{
                    $industry_index[$k]=false;
                }
            }
        }

        $hangye_index=[];
        $hangye_id=[];
        if ($workshop->hangye->isNotEmpty()){
           $hangye_id= $workshop->hangye->pluck('id')->all();

            foreach ($hangye as $k=> $item){
                if (in_array($item->id,$hangye_id)){
                    $hangye_index[$k]=true;
                }else{
                    $hangye_index[$k]=false;
                }
            }

        }
       $tags=WorkshopTag::all();
        $tags_id=[];
        $tags_index=[];
        if ($workshop->tags->isNotEmpty()){
            $tags_id= $workshop->tags->pluck('id')->all();

            foreach ($tags as $k=> $item){
                if (in_array($item->id,$tags_id)){
                    $tags_index[$k]=true;
                }else{
                    $tags_index[$k]=false;
                }
            }
        }
        $chengzhong_index=$chengzhong->search(function ($value,$index)use($workshop){
            return $value->id==$workshop->weight_id;
        });


        $park=Workshop::$parkMap;
        return response()->json([
            'code'=>'success',
            'message'=>'成功',
            'data'=>[
                'owndesc'=>$workshop->owndesc,
                'img'=>$img,
                'rent'=>$workshop->rent,
                'address'=>$workshop->address,
                'rent_price'=>$workshop->rent,
                'sure_rent'=>$workshop->rent,
                'owner'=>$workshop->owner,
                'owner_tel'=>$workshop->ownertel,
                'industry_id'=>implode(',',$industry_id),
                'industry_index'=>$industry_index,
                'hangye_id'=>implode(',',$hangye_id),
                'chengzhong'=>$workshop->weight_id ?? '',
                'chengzhong_index'=>$chengzhong_index ,
                'hangye_index'=>$hangye_index,
                'zhengjian_id'=>$workshop->certificates ?? '',
                'changfang_type'=>$workshop->changfang_type ?? '',
                'is_park'=>$workshop->is_park,
                'title'=>$workshop->title,
                'area'=>$workshop->area,
                'city_id'=>$workshop->p_city_id,
                'qu_id'=>$workshop->p_district_id,
                 'city_index'=>[$firstCity,$secondCity],
                'only_array'=>$onlyArray,
                'city'=>$city,
                'high'=>$workshop->high,
                'floor'=>$workshop->floor,
                'startTime'=>Carbon::parse($workshop->date_of_commission)->toDateString(),
                'endTime'=>Carbon::parse($workshop->commission_end_day)->toDateString(),
                'tags_id'=>implode(',',$tags_id),
                'tags_index'=>$tags_index
            ]
        ]);
    }
    /**
     * 是否更新
     */
    public function isEdit($id){
        $workshopLog=Workshop::where('id',$id)->where('updated_at','<',Carbon::now()->addDays(-10))->first();
        if ($workshopLog){
            return response()->json([
                'code'=>'success',
                'message'=>'请等待审核'
            ]);
        }else{
            return response()->json([
                'code'=>'error',
                'message'=>'请等待审核'
            ]);
        }
    }
    public function type(){
       $industry= WorkshopIndustry::all();
       $hangye=WorkshopHangye::all();
       $chengzhong=WorkshopWeight::all();
       $zhengjian=Workshop::$certificatesMap;
       $changfangType=Workshop::$changfangMap;
       $park=Workshop::$parkMap;
       $tags=WorkshopTag::all();
       return response()->json([
           'code' => 'success',
           'message' => '拒绝成功',
           'data' => compact('industry','hangye','chengzhong','zhengjian','changfangType','park','tags')
       ]);
    }
    public function uploadToken(){

       return response()->json([
            'code'=>'success',
           'message'=>'获取成功',
           'data'=>Storage::disk('qiniu')->uploadToken()
       ]);
    }

    /**
     * 查看是否收藏
     */
    public function isCollect(Request $request,$id){
        $user_workshop=UserWorkshop::where('user_id',$request->user('api')->id)->where('workshop_id',$id)->first();
        if ($user_workshop){
            return response()->json([
                'code'=>'success',
                'message'=>'已收藏'
            ]);
        }else{
            return response()->json([
                'code'=>'error',
                'message'=>'未收藏'
            ]);
        }
    }
    /**
     * 收藏房源
     */
    public function collect($id,Request $request){
        $user=$request->user('api');
        $workshop=Workshop::withoutGlobalScope('shenhe')->find($id);
        if (!$workshop){
            throw  new ApiException('房源不存在');
        }
       $userWorkshop= UserWorkshop::where('user_id',$user->id)->where('workshop_id',$workshop->id)->first();
       if ($userWorkshop){
           $userWorkshop->delete();
           return response()->json(
               [
                   'code'=>'success',
                   'message'=>'取消收藏成功',
                   'data'=>0
               ]
           );

       }else{
           $userWorkshop=new UserWorkshop();
           $userWorkshop->user_id=$user->id;
           $userWorkshop->workshop_id=$workshop->id;
           $userWorkshop->save();
           return response()->json(
               [
                   'code'=>'success',
                   'message'=>'收藏成功',
                   'data'=>1
               ]
           );
       }
    }

    /**
     * 收藏的盘源
     * @param Request $request
     * @return UserWorkshopCollection
     */
    public function collectPanyuan(Request $request){
            $user=$request->user('api');
            $search_key=$request->search_key;
            $userWorkshop=UserWorkshop::with(['workshop'=>function($query){
                $query->with(['hangye', 'qu', 'industry', 'tags'])->withoutGlobalScope('shenhe');
            }])->where('user_id',$user->id);
        if ($search_key) {
            $userWorkshop = $userWorkshop->whereHas('workshop', function ($builder) use ($search_key) {
                $builder->where('title','like', '%'.$search_key.'%');
            });
        }
        $userWorkshop=$userWorkshop->paginate(20);
            return new UserWorkshopCollection($userWorkshop);
    }
    /**
     * 置顶的盘源
     * @return WorkshopCollection
     */
    public function top(){
        $workshop=Workshop::with(['industry','tags'])->where('is_zhiding',1)->take(10)->get();

        return  new WorkshopCollection($workshop);
    }
    /**
     * 最新的盘源
     * @return WorkshopCollection
     */
    public function newList(){
        $workshop=Workshop::with(['industry','tags'])->orderBy('id','desc')->take(10)->get();
        return  new WorkshopCollection($workshop);
    }
    /**
     * 盘源详情
     */
    public function detail($id){
      $workshop=Workshop::withoutGlobalScope('shenhe')->with('industry','weight_type','qu', 'zhen')->find($id);
        return new WorkshopDetailResource($workshop);
    }

    /**
     * 城市选择
     * @return mixed
     */
    public function city(){
        $workshop=   City::with('children')->whereIn('parent_id',[102,101])->get();
        return $workshop;
    }
    /**
     * 管理员
     * 消息审批列表
     */
    public function shenpi(Request $request){
        $user=$request->user('api');
        $status=$request->status;
        //查出role_id
        $userShenhe=UserShenhe::orderBy('id','desc');
        if ($status==0){
          $userShenhe=  $userShenhe->where('status',UserShenhe::STATUS_SHENHE);
        }else{
            $userShenhe= $userShenhe->where('status','!=',UserShenhe::STATUS_SHENHE);
        }
        $userShenhe=$userShenhe->where('role_id',$user->role_id)->paginate(20);
        return new UserShenheCollection($userShenhe);
    }
    /**
     * 审核详情
     */
    public function shenheDetail($id,Request $request){
        $code=$request->code;

        $userShenhe=UserShenhe::with(['log'=>function($query){
            $query->orderBy('id','asc')->with(['user','role']);
        },'user','workshop'=>function($query){
           $query-> withoutGlobalScope('shenhe')->with(['industry','tags']);
        }]);
        if ($id==0){
            $userShenhe=$userShenhe->where('code',$code)->first();
        }else{
            $userShenhe=$userShenhe->where('id',$id)->first();
        }

        return new UserShenheResource($userShenhe);
    }
    /**
     * 申请查看房源业主
     */
    public function apply($id,Request $request,WorkshopInterFace $disSourceInterFace){

        $user=$request->user('api');
        $workshop=Workshop::find($id);
        $requestCode=$request->code;
        $userShenhe=MyApply::where('workshop_id',$workshop->id)
            ->where('type',MyApply::View_panyuan)
            ->where('user_id',$user->id)
            ->where('status',MyApply::SHENHE)
            ->first();
        if ($user->tiyan==1){
            return  response()->json(
                [
                    'code'=>'error',
                    'message'=>'体验账户不能申请'
                ]
            );
        }
        if ($userShenhe){
            return  response()->json(
                [
                    'code'=>'error',
                    'message'=>'正在审核中'
                ]
            );
        }
        $userWorkshopApply=UserWorkshopApply::where('workshop_id',$workshop->id)
            ->where('user_id','=',$user->id)
            ->first();
        if ($userWorkshopApply){
            return response()->json(
                [
                    'code'=>'error',
                    'message'=>'已经申请过'
                ]
            );
        }
        //查看当天是否超出20个
        $userApplyCount=UserWorkshopApply::whereDate('created_at','=',date("Y-m-d",time()))
            ->where('user_id','=',$user->id)
            ->count();

        \Log::info($userApplyCount);
        $code=$disSourceInterFace->getCode($workshop);
        if ($userApplyCount<6){//直接通过审核
            $users=  new UserShenhe();
            $users->type=UserShenhe::View_panyuan;
            $users->role_id=3;
            $users->title='查看业主通过';
            $users->status=UserShenhe::STATUS_TONGYI;
            $users->user_id=$user->id;
            if ($requestCode){
                $users->code=$requestCode;
            }else{
                $users->code=$code;
            }
            $users->workshop_id=$workshop->id;
            $users->save();
            $userWork=new UserWorkshopApply();
            $userWork->user_id=$user->id;
            $userWork->workshop_id=$workshop->id;
            $userWork->save();

            if ($requestCode){
                $myApply=MyApply::where('code',$requestCode)->first();
            }else{
                $myApply=    new MyApply();
                $myApply->code=$disSourceInterFace->getCode($workshop);
                $myApply->user_id=$user->id;
                $myApply->type=MyApply::View_panyuan;
                $myApply->workshop_id=$workshop->id;
            }
            $myApply->status=MyApply::SHENHE_PULISHED;
            $myApply->last_status=MyApply::SHENHE_PULISHED;
            $myApply->save();

            return response()->json(
                [
                    'code'=>'workshop',
                    'message'=>'申请成功'
                ]
            );
        }
        $userShenhe=new UserShenhe();
        $userShenhe->role_id=1;//采购审核
        $userShenhe->workshop_id=$workshop->id;
        $userShenhe->type=UserShenhe::View_panyuan;//查看业主
        $userShenhe->status=UserShenhe::STATUS_SHENHE;
        $userShenhe->title=$request->user('api')->name.'有一条申请查看业主信息待你审核，请尽快处理';
        if ($requestCode){
            $userShenhe->code=$requestCode;
        }else{
            $userShenhe->code=$code;
        }
        $userShenhe->user_id=$request->user('api')->id;
        $userShenhe->is_read=0;
        $userShenhe->save();
        if ($requestCode){
            $myApply=MyApply::where('code',$requestCode)->first();
            $myApply->status=MyApply::SHENHE;
        }else{
            $myApply=   new MyApply();
            $myApply->status=MyApply::SHENHE;
            $myApply->last_status=MyApply::SHENHE;
            $myApply->code=$disSourceInterFace->getCode($workshop);
            $myApply->user_id=$user->id;
            $myApply->type=MyApply::View_panyuan;
            $myApply->workshop_id=$workshop->id;
        }

        $myApply->save();
        UserShenheSum::addSum($user->id);
        $disSourceInterFace->applyWorkshopNotice($user,$workshop,$code);
        return response()->json(
            [
                'code'=>'success',
                'message'=>'申请成功,请等待审核'
            ]
        );
    }

    /**
     * @param $code
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 取消申请
     */
    public function cancel($code,Request $request){
       $myApply= MyApply::where('code',$code)->first();
       $myApply->status=MyApply::SHENHE_CANCEL;
       $myApply->save();
       $userShenheLog=new UserShenheLog();
       $userShenheLog->user_id=$request->user('api')->id;
       $userShenheLog->code=$myApply->code;
       $userShenheLog->status=UserShenheLog::STATUS_CANCEL;
       $userShenheLog->save();
       return response()->json([
           'code'=>'success',
           'message'=>'取消成功'
       ]);
    }
    /**
     * 是否能查看业主
     */
    public function  canView(Request $request){
        $user=$request->user('api');
        $workshop_id=$request->workshop_id;
        $workShop=Workshop::withoutGlobalScope('shenhe')->find(
            $workshop_id);
        $userWorkshopApply=UserWorkshopApply::where('workshop_id',$workshop_id)
            ->where('user_id','=',$user->id)
            ->first();
        if ($user->tiyan==1){
            return response()->json([
                'code'=>'error',
                'message'=>'不能查看',

            ]);
        }
        if ($userWorkshopApply||$user->id==$workShop->user_id||$user->role_id!=3){
            return response()->json([
                'code'=>'success',
                'message'=>'可以查看',
                'data'=>[
                    'mobile'=>$workShop->ownertel,
                    'address'=>$workShop->address,
                    'owner'=>$workShop->owner,
                    'longitude'=>$workShop->longitude,
                    'latitude'=>$workShop->latitude
                ]
            ]);
        }else{
            return response()->json([
                'code'=>'error',
                'message'=>'不能查看',

            ]);
        }
    }
    /**
     * 待更新页面
     */
    public function updatedList(Request $request){
        $user=$request->user('api');
        $workshop= Workshop::with('industry','tags')
            ->where('updated_at','<',Carbon::now()->addDays(-10))
            ->where('user_id',$user->id)->paginate(20);
        return  new WorkshopCollection($workshop);
    }
    /**
     * 即将过期的盘源
     */
    public function overdue(Request $request){
        $user=$request->user('api');
        $workshopOverdue=WorkshopOverdue::with(['workshop'=>function($query){
            $query->with('tags')->withoutGlobalScope('shenhe');
        }]);
        if ($user->role_id==3){
            $workshopOverdue->where('user_id',$user->id);
       }
       $workshopOverdue= $workshopOverdue->orderBy('id','desc')->paginate(20);

        return new WorkshopOverdueCollection($workshopOverdue);
    }
    /**
     *
     */
    public function notice($id,Request $request){
       $workshopOverdue= WorkshopOverdue::find($id);
        dispatch(new SendUserMini(\App\User::find($workshopOverdue->user_id),[
            'title'=>'过期盘源待更新',
            'url'=>'pages/detail/detail?id='.$workshopOverdue->workshop_id,
            'content_item'=>[
            ]
        ]));
        return \response()->json([
            'code'=>'success',
            'message'=>'发送提醒成功'
        ]);
    }
}
